/*
 * BrokerGui.java
 *
 * Created on January 12, 2007, 3:55 PM
 */

/**
 *
 * @author  vp200359
 */
package trader.gui;

import java.util.logging.*;
import java.util.*;
import java.text.*;
import trader.*;

public class BrokerGui extends javax.swing.JFrame {
    private BrokerController brokerController;
    private BrokerModel model;
    private String logName = "Broker Tool"; 
    private Logger logger = Logger.getLogger(logName); 
    private BrokerPanel[] panels= new BrokerPanel[10];        

    /** Creates new form BrokerGui */
    public BrokerGui() {
        initComponents();
        setTitle(this.getName());
    }

    public BrokerGui(BrokerModel model) { 
        this(); 
        this.model = model;
        //* Code to create and add the customer panel to the panels array.
        panels[0] = customerPanel1;
        panels[0].registerModel(model);
        //* Code to build AllCustomersPanel
        panels[1] = allCustomerTablePanel1;
        panels[1].registerModel(model);

        logger.addHandler(new GuiLogHandler());
    }

    public void addController(BrokerController controller){
        brokerController = controller;
        for(int i=0; panels[i] != null; i++)
            panels[i].registerController(controller);
    }

    public void displayObject(Object obj) {
        if (obj == null){
            System.out.println("Object display is null.");
            return;
        }
        if (obj instanceof Customer){
            panels[0].display(obj);
            panels[1].refresh();
        }
        if (obj instanceof Customer[]){
            panels[1].display(obj);
        }
        if (obj instanceof Portfolio){
            //TBD
        }
        if (obj instanceof Stock[]){
            //TBD
        }
        if (obj instanceof Exception) {
            // log the exception
            logger.severe(obj.toString());
        }
    }    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jTabbedPane1 = new javax.swing.JTabbedPane();
    allCustomerTablePanel1 = new trader.gui.AllCustomerTablePanel();
    customerPanel1 = new trader.gui.CustomerPanel();
    jpnLogPanel = new javax.swing.JPanel();
    jspnLogDisplay = new javax.swing.JScrollPane();
    jtaLogDisplay = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

    jTabbedPane1.addTab("All Customers", allCustomerTablePanel1);
    jTabbedPane1.addTab("Customer Details", customerPanel1);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jTabbedPane1)
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
    );

    jpnLogPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Log Panel"));

    jtaLogDisplay.setColumns(20);
    jtaLogDisplay.setRows(5);
    jspnLogDisplay.setViewportView(jtaLogDisplay);

    javax.swing.GroupLayout jpnLogPanelLayout = new javax.swing.GroupLayout(jpnLogPanel);
    jpnLogPanel.setLayout(jpnLogPanelLayout);
    jpnLogPanelLayout.setHorizontalGroup(
      jpnLogPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jpnLogPanelLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jspnLogDisplay, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
        .addContainerGap())
    );
    jpnLogPanelLayout.setVerticalGroup(
      jpnLogPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jpnLogPanelLayout.createSequentialGroup()
        .addComponent(jspnLogDisplay, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
        .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      .addComponent(jpnLogPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jpnLogPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents
    
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private trader.gui.AllCustomerTablePanel allCustomerTablePanel1;
  private trader.gui.CustomerPanel customerPanel1;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JTabbedPane jTabbedPane1;
  private javax.swing.JPanel jpnLogPanel;
  private javax.swing.JScrollPane jspnLogDisplay;
  private javax.swing.JTextArea jtaLogDisplay;
  // End of variables declaration//GEN-END:variables

    private class GuiLogHandler
    extends Handler
    {
        //Instance Vars
        private String logMessages;

        DateFormat df = DateFormat.getDateTimeInstance();

        /** Creates a new instance of GuiLogHandler */
        public GuiLogHandler() {
        }

        @Override
        public void flush() {
            throw new UnsupportedOperationException("Not supported yet.");
        }//flush()

        @Override
        public void close() throws SecurityException {
            logMessages = null;
        }//close()

        @Override
        public void publish(LogRecord logRecord) {
            
            // **  Date String
            Date date = new Date(logRecord.getMillis());
            String dateString = df.format(date);

            // **  Format the log record and publish it using the
            // **  jTextArea1.setText() method
            // **  The formatted output should display the log record:
            
            // **  Sequence number
            long   seqNumber   = logRecord.getSequenceNumber();
            // **  Log level
            String level       = logRecord.getLevel().getName();
            // **  Log message
            String message     = logRecord.getMessage();
            // **  Source class name
            String className   = logRecord.getSourceClassName();
            // **  Source method name
            String methodName  = logRecord.getSourceMethodName();

            // **  Formatted Message String
            logMessages = String.format("%05d::%s::%s\n" +
                                        "%s::%s\n" +
                                        "%s\n",
                                         seqNumber, dateString, level,
                                         className, methodName,
                                         message);
            // **  Display the formatted log message
            jtaLogDisplay.setText(logMessages);

        }//publish()

    }//GuiLogHandler Class

}//BrokerGui Class
